﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BTKERP.Model;
using BTKERP.BLL.Inventory;
using BTKERP.MVCWeb.Models;

namespace BTKERP.MVCWeb.Controllers
{
    public class GRNController : RestrictController
    {
        //
        // GET: /GRN/

        public ActionResult Index()
        {
            List<GRNMaster> masters = InventoryActivityMgt.GetGRNMasters();
            return View(masters);
        }

        //
        // GET: /GRN/Details/5

        public ActionResult Details(Guid id)
        {
            GRNManager manager = new GRNManager();
            GRNMaster master = new GRNMaster();
            List<GRNDetail> details = new List<GRNDetail>();
            master = InventoryActivityMgt.GetGRNMasterById(id);
            details = InventoryActivityMgt.GetGRNDetailByMasterId(id);
            manager.Master = master;
            manager.Details = details;
            return View(manager);
        }

        //
        // GET: /GRN/Create

        public ActionResult Create()
        {
            GRNMaster master = new GRNMaster();
            master.GRNDate = DateTime.Now.Date;
            ViewData["WarehouseList"] = GetWarehouseSelectList();
            ViewData["SupplierList"] = GetSupplierSelectList();
            return View(master);
        } 

        //
        // POST: /GRN/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here
                GRNMaster master = new GRNMaster();
                UpdateModel(master);
                master.GRNDate = DateTime.Now;
                master.Status = 0;
                InventoryActivityMgt.AddGRNMaster(master.GRNCode, master.GRNDate, master.SupplierId, master.ReceivedWarehouseId, master.Remark, master.Status, master.CreatedBy);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /GRN/Manage

        public ActionResult Manage(Guid id)
        {
            GRNManager manager = new GRNManager();
            GRNMaster master = new GRNMaster();
            List<GRNDetail> details = new List<GRNDetail>();
            master = InventoryActivityMgt.GetGRNMasterById(id);
            details = InventoryActivityMgt.GetGRNDetailByMasterId(id);
            manager.Master = master;
            manager.Details = details;
            List<Product> products = InventoryInfoMgt.GetProducts();
            //List<Product> availableProducts = new List<Product>();
            //foreach (Product p in products)
            //{
            //    bool ab = true;
            //    foreach (GRNDetail gd in details)
            //    {
            //        if (p.Id == gd.ProductId)
            //        {
            //            ab = false;
            //            break;
            //        }
            //    }
            //    if (ab)
            //        availableProducts.Add(p);
            //}
            //manager.Products = availableProducts;
            manager.Products = products;
            return View(manager);
            
        }

        [HttpPost]
        public ActionResult Manage(string submitButton, FormCollection formValue)
        {
            string masterId = formValue.Get("grnMasterId");
            string warehouseId = formValue.Get("warehouseId");
            switch (submitButton)
            {
                case "Post":
                    //GRNMaster master = InventoryActivityMgt.GetGRNMasterById(new Guid(masterId));
                     
                    InventoryActivityMgt.PostGRN(new Guid(masterId), new Guid(warehouseId));
                    return RedirectToAction("Index");
                case "Void":
                    int j = 0;
                    return RedirectToAction("Index");
                default:
                    return RedirectToAction("Manage/" + warehouseId);
            }
        }

        [HttpPost]
        public ActionResult AddReceivedProduct(FormCollection formValue)
        {
            string productIdStr = formValue.Get("txtProductId");
            string unitIdStr = formValue.Get("txtUnitId");
            string masterId = formValue.Get("txtGRNMasterId");
            string unitCostStr = formValue.Get("txtUnitCost"); 
            string qtyStr = formValue.Get("txtQty");
            decimal qtyD = decimal.Parse(qtyStr);
            decimal unitcostD = decimal.Parse(unitCostStr);
            decimal total = qtyD * unitcostD;

            try
            {
                InventoryActivityMgt.AddGRNDetail(new Guid(masterId),new Guid(productIdStr), new Guid(unitIdStr), qtyD, unitcostD, qtyD * unitcostD, total, false, 0, string.Empty, SessionEntity.Staff.UserName);
                return RedirectToAction("Manage/" + masterId);
            }
            catch
            {
                return RedirectToAction("Index");
            }

           
        }
        
        [HttpPost]
        public ActionResult EditReceivedProduct(FormCollection formValue)
        {
            string productIdStr = formValue.Get("txtProductIdEdit");
            string unitIdStr = formValue.Get("txtUnitIdEdit");
            string masterId = formValue.Get("txtGRNMasterIdEdit");
            string unitCostStr = formValue.Get("txtUnitCostEdit"); 
            string qtyStr = formValue.Get("txtQtyEdit");
            decimal qtyD = decimal.Parse(qtyStr);
            decimal unitcostD = decimal.Parse(unitCostStr);
            decimal total = qtyD * unitcostD;

            try
            {
                InventoryActivityMgt.UpdateGRNDetail(new Guid(masterId), new Guid(productIdStr), new Guid(unitIdStr), qtyD, unitcostD, qtyD * unitcostD, total, false, 0, string.Empty, SessionEntity.Staff.UserName);
                return RedirectToAction("Manage/" + masterId);
            }
            catch
            {
                return RedirectToAction("Index");
            }

            
        }

        [HttpPost]
        public ActionResult DeleteReceivedProduct(FormCollection formValue)
        {
            string productIdStr = formValue.Get("txtProductIdDelete");
            string masterId = formValue.Get("txtGRNMasterIdDelete");
            try
            {
                InventoryActivityMgt.PhysicalDelteGRNDetail(new Guid(masterId), new Guid(productIdStr));
                return RedirectToAction("Manage/" + masterId);
            }
            catch
            {
            }

            return RedirectToAction("Index");
        }

        //
        // GET: /GRN/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /GRN/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /GRN/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /GRN/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
